package SocialMediaSentiment;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;


public class TweetNGram{
    public static HashMap<String, Integer> getTriGrams() 
    {
        File file = new File("TrainSetAllWordsTweets.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text).append(System.getProperty("line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        char[] chararray = new char[contents.length()];
        contents.getChars(0, contents.length(), chararray, 0);
       
        StringTokenizer st = new StringTokenizer(contents.toString(), "\r\n\t\f,.!$%^&*()[]{}-=\\;<>/?");
        String word = null;
        ArrayList<String> tweets = new ArrayList<String>();
        ArrayList<Integer> sentiment = new ArrayList<Integer>();
        StringBuffer tweet = new StringBuffer();
        int number = st.countTokens();
        for (int counter = 0; number-counter > 0; counter++ )
        {
        	
	        word = st.nextToken();
	        if( word.contains("NEXTTWEEEEET") )
	        {
	        	//add all words to new tweed
	        	int sentimentvalue;
	        	
	        	if(word.length() == 13 ) //positief of 0
	        		sentimentvalue = Character.getNumericValue(word.charAt(0));
	        	else
	        	{
	        		sentimentvalue = -1*Character.getNumericValue(word.charAt(3));
	        	}
	        	
	        	tweets.add(tweet.toString());
	        	sentiment.add(sentimentvalue);
	        	//remove tweet
	        	tweet.delete(0, tweet.length());
	        }
	        else
	        {
	        	//add word to tweet
	        	tweet.append( word + " " );
	        }
	       
        }
        
        ArrayList<String> ngrams = new ArrayList<String>();
        HashMap<String, Integer> allNGrams = new HashMap<String, Integer>();
        StringTokenizer messageNGram;
        StringTokenizer messageNGram2;
        StringTokenizer messageNGram3;
        //For each message, make the ngrams. (tri-grams)
        for (String message : tweets)
        {
        	int index = tweets.indexOf(message);
        	messageNGram =  new StringTokenizer(message, " ");
        	messageNGram2 = new StringTokenizer(message, " ");
        	messageNGram2.nextToken();
        	messageNGram3 = new StringTokenizer(message, " ");
        	messageNGram3.nextToken();
        	messageNGram3.nextToken();
        	String part1,part2,part3;
        	number = messageNGram.countTokens();
        	String trigram = "";
        	for(int i=0; number-i>2;i++)
        	{
				part1 = messageNGram.hasMoreTokens() ? messageNGram.nextToken():"";
				part2 = messageNGram2.hasMoreTokens() ? messageNGram2.nextToken():"";
				part3 = messageNGram3.hasMoreTokens() ? messageNGram3.nextToken():"";
				trigram = part1+" "+part2+" "+part3;
				if (!trigram.isEmpty())
				{
					ngrams.add(trigram);
				}
				
        	}
        	
        	allNGrams.put(message, sentiment.get(index));
        	ngrams.clear();
        }
        //System.out.println(allNGrams);
        /*
        try{
      	  // Create file
        	
      	  FileWriter fstream = new FileWriter("out.txt");
      	  BufferedWriter out = new BufferedWriter(fstream);
            for( String printTweets : allNGrams )
            {
          	  out.write(printTweets);
          	  out.newLine();
            }
            //Close the output stream
      	  out.close();
      	  }catch (Exception e){//Catch exception if any
      	  System.err.println("Error: " + e.getMessage());
      	  }
      	  */
		return allNGrams;
        

    }
    
    
    
}
